package com.niji.littly_donkey_android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.bean.LoadFile;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class LoadPicAdatper extends RecyclerView.Adapter<LoadPicAdatper.MyViewHolder> {

    Context context;

    List<LoadFile> loadFiles = null;

    View view;

    int picNum = 3;

    public LoadPicAdatper(Context context,List<LoadFile> loadFiles){
        this.context = context;
        this.loadFiles = loadFiles;
    }

    public LoadPicAdatper(Context context,List<LoadFile> loadFiles,int picNum){
        this.context = context;
        this.loadFiles = loadFiles;
        this.picNum = picNum;
    }

    public interface OnItemClickListener{
        void click(View view,int position);

        void del(View view);
    }

    OnItemClickListener listener;

    public void setListener(OnItemClickListener listener){
        this.listener = listener;
    }



    @NonNull
    @NotNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.item_comment_picture,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MyViewHolder holder, int position) {

        if(position == 0 && loadFiles.get(position).getBitmap() == null){
            holder.ivPic.setImageResource(R.mipmap.addpicture);
            holder.ivPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.click(v,position);
                }
            });
            holder.ivDel.setVisibility(View.INVISIBLE);

        }else {
            holder.ivPic.setImageBitmap(loadFiles.get(position).getBitmap());
            holder.ivDel.setVisibility(View.VISIBLE);

        }
        holder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(loadFiles.get(position).isUpload()){
                    Toast.makeText(context,"该图片已上传",Toast.LENGTH_SHORT).show();
                }else {
                    loadFiles.remove(position);
                    if (loadFiles.size() == picNum - 1 && loadFiles.get(0).getBitmap() != null){
                        loadFiles.add(0,new LoadFile());
                    }
                    notifyDataSetChanged();
                    if(listener !=null){
                        listener.del(v);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return loadFiles.size();
    }


    static class MyViewHolder extends RecyclerView.ViewHolder{

        ImageView ivPic;

        Button ivDel;

        View view;

        public MyViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            ivPic = itemView.findViewById(R.id.show_photo);
            ivDel = itemView.findViewById(R.id.del_photo);
            this.view = itemView;
        }
    }
}
